package org.magnerfuller.imtoolkit.server.service;

import org.magnerfuller.imtoolkit.server.naming.NamedModuleDirectory;

public class ServiceFactory {
  
  private static ServiceFactory me;
  
  private NamedModuleDirectory directory = new NamedModuleDirectory();
  
  public static ServiceFactory getInstance() {
    
    if(me == null) {
      me = new ServiceFactory();
    }
    
    return me;
  }
  
  public ServiceBackend createService(ServiceDescription desc) {
    ServiceBackend service;
    try {
      Class module = directory.lookupModule(desc.getServiceData().get("type"));
      service = (ServiceBackend)module.newInstance();
      service.initialize(desc.getServiceData());
    } catch(Exception e) {
      e.printStackTrace();
      service = null;
    }
    return service;
  }
  
  public void addServiceModule(Class module) {
    directory.addModule(module);
  }
  
  public void addServiceModule(Class module, String moduleName) {
    directory.addModule(module, moduleName);
  }
  
}
